package com.yxq.mdt.app.modules.personal.ui.fragment;

import android.content.Intent;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.base.BaseFragment;
import com.yxq.mdt.app.base.ibase.IBaseContract;
import com.yxq.mdt.app.modules.goodsdetail.bean.CollectGoodsBean;
import com.yxq.mdt.app.modules.goodsdetail.model.GoodsModel;
import com.yxq.mdt.app.modules.goodsdetail.ui.GoodsDetailTabVerticalActivity;
import com.yxq.mdt.app.modules.personal.adapter.CollectGoodsListAdapter;
import com.yxq.mdt.app.net.ApiModel;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @author L
 * @time 2019-11-19
 * @describe 收藏-商品
 */
public class CollectGoodsFragment extends BaseFragment {
    public static final String EXTRA_TEXT = "extra_text";
    @BindView(R.id.social_state_recyclerView)
    RecyclerView socialStateRecyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    CollectGoodsListAdapter collectGoodsListAdapter;
    private List<CollectGoodsBean.ListBean> mainSocialContactBeans;

    @Override
    protected int attachLayoutId() {
        return R.layout.fragment_collect_tab;
    }

    @Override
    protected IBaseContract.IBasePresenter getPresenter() {
        return null;
    }

    @Override
    public void initView(View view) {
        refreshLayout.setOnRefreshListener(refreshLayout -> {
            page = 1;
            GoodsModel.favoriteList(1, new ApiModel.CommonCallback<CollectGoodsBean>() {
                @Override
                public void onSuccess(CollectGoodsBean collectGoodsBean) {
                    refreshLayout.finishLoadMore();
                    refreshLayout.finishRefresh();
                    bindData(collectGoodsBean);
                }

                @Override
                public void onError(String errMsg) {
                    refreshLayout.finishLoadMore();
                    refreshLayout.finishRefresh();
                }
            });
        });
        refreshLayout.setOnLoadMoreListener(refreshLayout -> {
            page++;
            GoodsModel.favoriteList(page, new ApiModel.CommonCallback<CollectGoodsBean>() {
                @Override
                public void onSuccess(CollectGoodsBean collectGoodsBean) {
                    refreshLayout.finishLoadMore();
                    refreshLayout.finishRefresh();
                    bindData(collectGoodsBean);
                }

                @Override
                public void onError(String errMsg) {
                    refreshLayout.finishLoadMore();
                    refreshLayout.finishRefresh();
                }
            });
        });
        mainSocialContactBeans = new ArrayList<>();
        socialStateRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        collectGoodsListAdapter = new CollectGoodsListAdapter(R.layout.layout_collect_goods_item_layout, mainSocialContactBeans);
        socialStateRecyclerView.setAdapter(collectGoodsListAdapter);
        collectGoodsListAdapter.setOnItemClickListener((adapter, view1, position) -> {
            Intent it = new Intent(mContext, GoodsDetailTabVerticalActivity.class);
            it.putExtra("id", collectGoodsListAdapter.getItem(position).getGoods_id());
            startActivity(it);
        });
        collectGoodsListAdapter.setEmptyView(R.layout.layout_empty_view, socialStateRecyclerView);
        collectGoodsListAdapter.isUseEmpty(false);
    }

    @Override
    public void initData() {
        GoodsModel.favoriteList(1, new ApiModel.CommonCallback<CollectGoodsBean>() {
            @Override
            public void onSuccess(CollectGoodsBean collectGoodsBean) {
                bindData(collectGoodsBean);
            }

            @Override
            public void onError(String errMsg) {
            }
        });
    }

    private void bindData(CollectGoodsBean collectGoodsBean) {
        if (page == 1) {
            collectGoodsListAdapter.setNewData(collectGoodsBean.getList());
        } else {
            if (collectGoodsBean.getList() != null && collectGoodsBean.getList().size() != 0) {
                collectGoodsListAdapter.addData(collectGoodsBean.getList());
            } else {
                refreshLayout.finishLoadMoreWithNoMoreData();
            }
        }
        collectGoodsListAdapter.isUseEmpty(!(collectGoodsListAdapter.getData().size() > 0));
    }

    @Override
    public void setData(@Nullable Object data) {

    }
}
